package com.wandian.news.adapter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.tools.CSJADTools;
import com.mylib.libcore.tools.GDTADTools;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.UIUtils;
import com.qq.e.ads.nativ.express2.NativeExpressADData2;
import com.wandian.news.R;
import com.wandian.news.bean.NewsItemBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;



public class NewsHotRecycleAdapter extends BaseMultiItemQuickAdapter<NewsItemBean, BaseViewHolder>  {
    public static final  int ONE_PIC = 0;
    public static final  int AD_PIC = 1;
    public static final  int AD_TWO_PIC = 2;
    private String value ="guonei";
    private Context context;
    private List<NativeExpressADData2> listGDTAD = new ArrayList<>();

    private List<TTNativeExpressAd> ads = new ArrayList<>();

    public List<TTNativeExpressAd> getAds() {
        return ads;
    }

    public void setAds(List<TTNativeExpressAd> ads) {
        this.ads = ads;
    }

    public List<NativeExpressADData2> getListGDTAD() {
        return listGDTAD;
    }

    public void setListGDTAD(List<NativeExpressADData2> listGDTAD) {
        this.listGDTAD = listGDTAD;
    }

    public NewsHotRecycleAdapter(List<NewsItemBean> data, Context context,String value) {
        super(data);
        this.context = context;
        this.value = value;
        addItemType(ONE_PIC, R.layout.news_item_zixun_style_one);
        addItemType(AD_PIC, R.layout.news_item_news_layout_ad_1);
        addItemType(AD_TWO_PIC, R.layout.news_item_news_layout_ad_2);
    }


    @Override
    protected void convert(final BaseViewHolder helper, NewsItemBean item) {
        try {
            int viewType = getDefItemViewType(helper.getLayoutPosition());

            switch (viewType) {
                case ONE_PIC:
                    doOnePicture(helper, item);
                    break;
                case AD_PIC:
                    doADOne(helper);
                    break;
                case AD_TWO_PIC:
                    doADTwo(helper);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void doOnePicture(BaseViewHolder helper, final NewsItemBean item) {
        try {
            ImageView iv_zx_1 = helper.getView(R.id.iv_zx_1);
            if(!UIUtils.isEmpty(item.getPicUrl())){
                GlideUtil.intoNewsPic(mContext,item.getPicUrl(),iv_zx_1);
            }
            TextView tv_time = helper.getView(R.id.tv_time);
            TextView tv_form = helper.getView(R.id.tv_form);
            TextView tv_zx_title = helper.getView(R.id.tv_zx_title);
            if(!UIUtils.isEmpty(item.getCtime())){
                if(item.getCtime().length()>10){
                    tv_time.setText(""+item.getCtime().substring(0,10));
                }else {
                    tv_time.setText(""+item.getCtime());
                }
            }
            if(!UIUtils.isEmpty(item.getDescription())){
                tv_form.setText(item.getDescription()+"");
            }
            if(!UIUtils.isEmpty(item.getTitle())){
                tv_zx_title.setText(item.getTitle());
            }
            RelativeLayout rl_item = helper.getView(R.id.rl_item);
            rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(!UIUtils.isEmpty(item.getUrl())){
                        if(item.getUrl().contains(".html")){
                            Bundle bundle = new Bundle();
                            bundle.putString("news_form_time",item.getDescription()+"  "+item.getCtime());
                            bundle.putString("newsID",item.getUrl());
                            bundle.putString("newsTitle",item.getTitle());
                            bundle.putString("newsDescribe",item.getDescription());
                            bundle.putString("value", value);
                            ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_NEWS_TOUTIAO_DETAIL).with(bundle).navigation();
                        }else {
                            Bundle bundle = new Bundle();
                            bundle.putString("url",item.getUrl());
                            bundle.putString("title",item.getTitle());
                            bundle.putString("type","微信头条");
                            ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_WEB).with(bundle).navigation();
                        }
                    }
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void doADOne(BaseViewHolder helper) {
        RelativeLayout rl_item = helper.getView(R.id.rl_item);
        RelativeLayout ad_container = helper.getView(R.id.ad_container);
        if(!UIUtils.isEmpty(ads) && ads.size()>0){
            rl_item.setVisibility(View.VISIBLE);
            try {
                Random random = new Random();
                int index =random.nextInt(ads.size());
                CSJADTools.bindAdListener(ads.get(index),ad_container);
                ads.get(index).render();
                notifyItemChanged(helper.getLayoutPosition());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            rl_item.setVisibility(View.GONE);
        }
    }

    private void doADTwo(BaseViewHolder helper ) {
        RelativeLayout rl_item = helper.getView(R.id.rl_item);
        FrameLayout ad_container = helper.getView(R.id.ad_container);
        if(!UIUtils.isEmpty(listGDTAD) && listGDTAD.size()>0){
            rl_item.setVisibility(View.VISIBLE);
            Random random = new Random();
            int index =random.nextInt(listGDTAD.size());
            GDTADTools.renderGDTAd(listGDTAD.get(index),ad_container);
        }else {
            rl_item.setVisibility(View.GONE);
        }
    }

}
